package com.mrzhou.thread.created;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 通过{@link Thread}创建线程实例
 * @author qiang.zhou
 */
@Slf4j
public class ExtendsThread extends Thread {

    @Override
    public void run() {
        log.info("继承Thread创建线程实例");
    }

    public static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws Exception {
        Integer[] root = {3,9,20,null,null,15,7};
        List<List<Integer>> list = tireForeach(root);
        System.out.println(objectMapper.writeValueAsString(list));
    }

    public static List<List<Integer>> tireForeach(Integer[] root) {
        List<List<Integer>> list = new ArrayList<>();
        int tire = 0;
        int point = 0;
        while(true) {
            // 当前层最大节点数量
            int max = (int) Math.pow(2, tire);
            List<Integer> node = new ArrayList<>();
            for(int i = 0; i < max; i++) {
                if (null != root[point]) {
                    node.add(root[point]);
                }
                point++;
            }
            list.add(node);
            tire++;
            if (point == root.length) {
                break;
            }
        }
        return list;
    }

}
